<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">
        后台管理系统
      </div>
      <el-form
        ref="register"
        :model="param"
        :rules="rules"
        label-width="0px"
        class="ms-content"
      >
        <el-form-item prop="username">
          <el-input
            v-model="param.username"
            placeholder="username"
          >
            <template #prepend>
              <el-button icon="el-icon-user" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="param.password"
            type="password"
            placeholder="password"
          >
            <template #prepend>
              <el-button icon="el-icon-lock" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="param.confirmPassword"
            type="password"
            placeholder="confirm password"
          >
            <template #prepend>
              <el-button icon="el-icon-lock" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input
            v-model="param.email"
            type="email"
            placeholder="email"
          >
            <template #prepend>
              <el-button icon="el-icon-star-on" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="param.code"
            placeholder="Verification Code"
          >
            <template #prepend>
              <el-button
                v-show="showTime"
                type="primary"
                @click="sendEmail(param.email)"
              >
                验证码
              </el-button>
              <el-button
                v-show="!showTime"
                type="primary"
              >
                {{ sendTime }}秒
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button
            type="primary"
            @click="submitForm()"
          >
            注册
          </el-button>
        </div>
        <p style="margin-left: 240px"><router-link to="/login">登录</router-link></p>
      </el-form>
    </div>
  </div>
</template>

<script>
    import {register} from "../api/index"
    import {sendEmail} from "../api/index"
export default {
    data() {
        var validateCheckPass = (rule,value,callback) => {
            if (value === "") {
                callback(new Error("请再次密码"));
            } else if (value !== this.param.password) {
                callback(new Error("两次输入密码不一致！"));
            } else {
                callback();
            }
        };
        return {
          showTime: true, /* 布尔值，通过v-show控制显示‘获取按钮’还是‘倒计时’ */
          sendTime: '', /* 倒计时 计数器 */
          timer: null,
            param: {
                username: "",
                password: "",
                confirmPassword:"",
                email: "",
            },
            rules: {
                username: [
                    { required: true, message: "请输入用户名", trigger: "blur" },
                    {min:2,max:10,message: "长度在2 到 10 个字符"}
                ],
                password: [
                    { required: true, message: "请输入密码", trigger: "blur" }
                ],
                confirmPassword:[
                    {required: true, message: "请再次确认密码", trigger: "blur"},
                    {validator: validateCheckPass, trigger: "blur" },
                ],
                email: [
                    {required: true, message: "请输入邮箱",trigger:"blur"}
                ],
              code:[
                {required: true, message: "请输入验证码",trigger:"blur"}
              ]
            }
        };
    },
    created() {
        this.$store.commit("clearTags");
    },
    methods: {
      sendEmail() {
        const TIME_COUNT = 60; //  更改倒计时时间
        if (!this.timer) {
          this.sendTime = TIME_COUNT;
          this.showTime = false;
          this.timer = setInterval(() => {
            if (this.sendTime > 0 && this.sendTime <= TIME_COUNT) {
              this.sendTime--;
            } else {

              this.showTime = true;
              clearInterval(this.timer); // 清除定时器
              this.timer = null;
              sessionStorage.setItem("code", null);
            }
          }, 1000);
        }
        sendEmail(this.param.email).then(res => {
          this.$message.success("发送成功");
          sessionStorage.setItem("code", res.data);
        }).catch(() => {
          this.$message.error("发生错误，请重试")});
      },
        submitForm() {
            this.$refs.register.validate(valid => {
                if (valid) {
                  if(this.param.code !=sessionStorage.getItem("code")){
                    this.$message.error("验证码错误");
                    return false;
                  }
                    register(this.param.username,this.param.password,this.param.email).then(res => {
                       console.log(res);
                        this.$message.success("注册成功");
                        localStorage.setItem("ms_username", this.param.username);
                        this.$router.push("/dashboard");
                    }).catch(() => {
                        this.$message.error("发生错误，请重试")});
                } else {
                    this.$message.error("请按照格式输入账号和密码");
                    return false;
                }
            });
        }
    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>
